use ${dirTemp}/compustat_quarterly_markups_final, clear

* Normalize MP shocks 
* ========================
foreach shock of varlist ffr3shockTightWeighted policyShockTightWeighted ffr3shockTightAllWeighted ffr3shockTightSignWeighted ffr3shockTightNoQEWeighted ffr3shockTightGBWeiUncorr {
	egen sd = sd(`shock')
	replace `shock' = `shock'/sd
	drop sd 
} 

* Define function for panel-LP
* =========================================

cap program drop panel_lp
program define panel_lp

preserve 

global shock = "`3'"
global fixedEffects = "id i.naicsq2#i.date_q"  
global markup = "`1'" 
global sticki = "`2'"
global controlsSwitch = "`4'"
global sampleName = "`5'" 
global name = "`6'"
macro drop _1
macro drop _2
macro drop _3
macro drop _4
macro drop _5 
 
if "${sampleName}" == "dropGR" { 
	drop if date_q>=yq(2008,3) & date_q<=yq(2009,2)
	replace date_q = date_q + 40 if  date_q>=yq(2008,3)  
	global sampleAddFilename = ""
}
else if "${sampleName}" == "dropZLB" { 
	replace ${shock} = . if date_q > yq(2008,4)
	global sampleAddFilename = "_dropZLB"
}
else if "${sampleName}" == "preGR" { 
	drop if date_q>=yq(2008,3) 
	global sampleAddFilename = "_preGR"
}
else if "${sampleName}" == "postGR" { 
	drop if date_q<yq(2009,2) 
	global sampleAddFilename = "_postGR"
}
else if "${sampleName}" == "inclGR" { 
	global sampleAddFilename = "_inclGR"
} 
else {
	ERROR_SAMPLE
}
if "$controlsSwitch" == "controlsOn" {
	global controls = "c.Llatq_w#c.${shock} Llatq_w c.Llev_w#c.${shock} Llev_w c.Lliq_w#c.${shock} Lliq_w"
	global controlsAddName = "_controls"
}
else if "$controlsSwitch" == "controlsOff" {
	global controls = ""
	global controlsAddName = ""
} 
else {
	ERROR_CONTROLS
} 
 
gen const = 1   
xtset id date_q 

* Space for IRFs
*************************
global H = 16

matrix define b = J($H+1,1,.) 
matrix define se = J($H+1,1,.)

* Dynamic markup regressions / IRF
*****************************************
forvalues h = 0(1)$H { 
di "*****************************************************"
di "FORECAST HORIZON: `h'"
di "*****************************************************"
di " "
  
	cap drop LHS 
	gen LHS = (F`h'.${markup}  - L.${markup})*100
	
	cap drop ctrl
	gen ctrl = L.${markup}  - L2.${markup} 
 
	reghdfe LHS c.${sticki}#c.${shock} $controls ctrl ///
		, absorb(${fixedEffects}) vce(cluster id date_q) ///
		nosample noomitted noempty nocons vsquish   
		
	matrix define b[`h'+1,1] = _b[c.${sticki}#c.${shock}]
	matrix define se[`h'+1,1] = _se[c.${sticki}#c.${shock}]

}   

* Save results to csv
**************************
svmat b
svmat se
keep b1 se1
rename b1 irf
rename se1 irfse
keep irf irfse
keep if _n <= $H+1
outsheet using ${dirOutput}/${name}.csv, replace delimiter(",")

restore 

end


* Define function for panel-LP-IV
* =========================================

cap program drop panel_lpiv
program define panel_lpiv

preserve 

global shock = "`3'"
global fixedEffects = "id i.naicsq2#i.date_q"  
global markup = "`1'" 
global sticki = "`2'"
global controlsSwitch = "`4'"
global sampleName = "`5'" 
global name = "`6'"
macro drop _1
macro drop _2
macro drop _3
macro drop _4
macro drop _5 
 
if "${sampleName}" == "dropGR" { 
	drop if date_q>=yq(2008,3) & date_q<=yq(2009,2)
	replace date_q = date_q + 40 if  date_q>=yq(2008,3)  
	global sampleAddFilename = ""
}
else if "${sampleName}" == "dropZLB" { 
	replace ${shock} = . if date_q > yq(2008,4)
	global sampleAddFilename = "_dropZLB"
}
else if "${sampleName}" == "preGR" { 
	drop if date_q>=yq(2008,3) 
	global sampleAddFilename = "_preGR"
}
else if "${sampleName}" == "postGR" { 
	drop if date_q<yq(2009,2) 
	global sampleAddFilename = "_postGR"
}
else if "${sampleName}" == "inclGR" { 
	global sampleAddFilename = "_inclGR"
} 
else {
	ERROR_SAMPLE
}
if "$controlsSwitch" == "controlsOn" {
	global controls = "c.Llatq_w#c.DGS1 Llatq_w c.Llev_w#c.DGS1 Llev_w c.Lliq_w#c.DGS1 Lliq_w"
	global controlsAddName = "_controls"
}
else if "$controlsSwitch" == "controlsOff" {
	global controls = ""
	global controlsAddName = ""
} 
else {
	ERROR_CONTROLS
} 
 
gen const = 1   
xtset id date_q 

* Space for IRFs
*************************
global H = 16

matrix define b = J($H+1,1,.) 
matrix define se = J($H+1,1,.)

* Dynamic markup regressions / IRF
*****************************************
forvalues h = 0(1)$H { 
di "*****************************************************"
di "FORECAST HORIZON: `h'"
di "*****************************************************"
di " "
  
	cap drop LHS 
	gen LHS = (F`h'.${markup}  - L.${markup})*100
	
	cap drop ctrl
	gen ctrl = L.${markup}  - L2.${markup} 
 
	ivreghdfe LHS (c.${sticki}#c.DGS1 = c.${sticki}#c.${shock}) $controls ctrl ///
		, absorb(${fixedEffects}) cluster(id date_q) 

		matrix define b[`h'+1,1] = _b[c.${sticki}#c.DGS1] 
	matrix define se[`h'+1,1] = _se[c.${sticki}#c.DGS1]   

}   

* Save results to csv
**************************
svmat b
svmat se
keep b1 se1
rename b1 irf
rename se1 irfse
keep irf irfse
keep if _n <= $H+1
outsheet using ${dirOutput}/${name}.csv, replace delimiter(",")

restore 

end



* Estimate
* =============

panel_lp lmarkup_PF dur_avg3_std_w  ffr3shockTightWeighted "controlsOff" "dropGR" "_Figure4a-1"
panel_lp lmarkup_PF freq_avg3_std_w ffr3shockTightWeighted "controlsOff" "dropGR" "_Figure4a-2" 

panel_lp lmarkup_PF dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_Figure4b-1"
panel_lp lmarkup_PF freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_Figure4b-2" 
 
panel_lp mshare2t dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR"   "_Figure4c-1" 
panel_lp mshare2t freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR"   "_Figure4c-2"

panel_lp lmarkup_AP dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_Figure4d-1" 
panel_lp lmarkup_UC dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_Figure4d-2" 

panel_lpiv lmarkup_PF dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureC9c-1"
panel_lpiv lmarkup_PF freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureC9c-2"

panel_lpiv mshare2t dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureC9d-1"
panel_lpiv mshare2t freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureC9d-2"

panel_lp lmarkup_AP dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1a-1"
panel_lp lmarkup_AP freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1a-2"

panel_lp lmarkup_UC dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1b-1"
panel_lp lmarkup_UC freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1b-2"

panel_lp lmarkup_PF_TL dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1c-1"
panel_lp lmarkup_PF_TL freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1c-2"

panel_lp lmarkup_PF_CS dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1d-1"
panel_lp lmarkup_PF_CS freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1d-2"

panel_lp lmarkup_PF_sga dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1e-1"
panel_lp lmarkup_PF_sga freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1e-2"

panel_lp mshare2t dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1f-1"
panel_lp mshare2t freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD1f-2"

panel_lp lmarkup_PF_lowsales dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2a-1"
panel_lp lmarkup_PF_lowsales freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2a-2"

panel_lp lmarkup_PF_excgro dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2b-1"
panel_lp lmarkup_PF_excgro freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2b-2"

panel_lp lmarkup_PF_tr1 dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2c-1"	
panel_lp lmarkup_PF_tr1 freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2c-2"

panel_lp lmarkup_PF_min16 dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2d-1"
panel_lp lmarkup_PF_min16 freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD2d-2"

panel_lp lmarkup_PF dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "preGR" "_FigureD2e-1"
panel_lp lmarkup_PF freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "preGR" "_FigureD2e-2"

panel_lp lmarkup_PF dur_avg3_std_w  ffr3shockTightWeighted "controlsOn" "inclGR" "_FigureD2f-1"
panel_lp lmarkup_PF freq_avg3_std_w ffr3shockTightWeighted "controlsOn" "inclGR" "_FigureD2f-2"

panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD3a1-1"
panel_lp lmarkup_PF dur_avg3_std_w policyShockTightWeighted "controlsOn" "dropGR" "_FigureD3a1-2" 
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightGBWeiUncorr "controlsOn" "dropGR" "_FigureD3a1-3" 

panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightSignWeighted "controlsOn" "dropGR" "_FigureD3a2-1"
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightAllWeighted "controlsOn" "dropGR" "_FigureD3a2-2" 
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightNoQEWeighted "controlsOn" "dropGR" "_FigureD3a2-3" 

panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightWeighted "controlsOn" "dropGR" "_FigureD3b1-1"
panel_lp lmarkup_PF dur_avg3_std_w policyShockTightWeighted "controlsOn" "dropGR" "_FigureD3b1-2" 
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightGBWeiUncorr "controlsOn" "dropGR" "_FigureD3b1-3" 

panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightSignWeighted "controlsOn" "dropGR" "_FigureD3b2-1"
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightAllWeighted "controlsOn" "dropGR" "_FigureD3b2-2" 
panel_lp lmarkup_PF dur_avg3_std_w ffr3shockTightNoQEWeighted "controlsOn" "dropGR" "_FigureD3b2-3" 
